import 'package:flutter/material.dart';
import 'package:flutterdemo01/demo4/Demo4ListPage.dart';

main() {
  runApp(Demo4());
}

class Demo4 extends StatelessWidget {
  const Demo4({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.greenAccent),
      home: Scaffold(
        body: MyBody(),
      ),
      onGenerateRoute: (RouteSettings settings) {
        // 统一处理
        print('方法执行了吗');

        final String? name = settings.name;
        print('name:::$name');
        final Function? pageContentBuilder = myrotues[name];
        if (pageContentBuilder != null) {
          if (settings.arguments != null) {
            final Route route = MaterialPageRoute(
                builder: (context) =>
                    pageContentBuilder(context, arguments: settings.arguments));
            return route;
          } else {
            final Route route = MaterialPageRoute(
                builder: (context) => pageContentBuilder(context));
            return route;
          }
        }
      }, //必须加上这一行，固定写法

      routes: myrotues,
    );
  }
}

class MyBody extends StatefulWidget {
  const MyBody({Key? key}) : super(key: key);

  @override
  _MyBodyState createState() => _MyBodyState();
}

class _MyBodyState extends State<MyBody> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        SizedBox(
          height: 300,
        ),
        ElevatedButton(onPressed: () {
          Navigator.pushNamed(context, '/msg', arguments: {'msg':'323'});
        }, child: Text('点击跳转到列表页面'))
      ],
    );
  }
}

final myrotues = {
  '/msg': (context, {arguments}) {
    print('arguements${arguments}');
    return Demo4ListPage(arguments: arguments);
  },
};

// var onGenerateRoute = (RouteSettings settings) {
//   // 统一处理
//
//   final String? name = settings.name;
//   print('name:::$name');
//   final Function? pageContentBuilder = rou[name];
//   if (pageContentBuilder != null) {
//     if (settings.arguments != null) {
//       final Route route = MaterialPageRoute(
//           builder: (context) =>
//               pageContentBuilder(context, arguments: settings.arguments));
//       return route;
//     } else {
//       final Route route =
//       MaterialPageRoute(builder: (context) => pageContentBuilder(context));
//       return route;
//     }
//   }
// };
